import {Component, OnInit} from '@angular/core';
import {ModelService} from '../../../../../service/model.service';
import {Model} from '../../../../../entity/model.model';
import {BladeContainerBase} from '../../../../../shared/blade/blade-container-base';
import {BladeData} from '../../../../../shared/blade/blade-data';
import {Blade} from '../../../../../shared/blade/blade.model';
import {Command} from '../../../../../shared/toolbar.service';
import {DialogResult} from '../../../../../shared/dialog.service';
import {StandardCurveEditComponent} from '../standard-curve-edit/standard-curve-edit.component';
import {WindPowers} from '../../../../../entity/wind-powers.model';
import {WindPowersService} from '../../../../../service/wind-powers.service';
import {Scheme} from '../../../../../entity/scheme.model';

@Component({
  selector: 'app-standard-curve-list',
  templateUrl: './standard-curve-list.component.html',
  styleUrls: ['./standard-curve-list.component.css']
})
export class StandardCurveListComponent extends BladeContainerBase<Scheme> {

  /**
   * 风功率曲线集合
   *
   * @type {WindPowers[]}
   * @memberof StandardCurveListComponent
   */
  windPowersList: WindPowers[] = [];

  /**
   * 当前风功率
   *
   * @type {WindPowers}
   * @memberof StandardCurveListComponent
   */
  currentWindPower: WindPowers;

  constructor(
    private modelService: ModelService,
    private windPowersService: WindPowersService
  ) {
    super()
  }

  ngOnInit() {
    this.commands = [
      new Command('add', '添加', 'fa fa-plus', 'setofkey:insert', blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command('edit', '编辑', 'fa fa-edit', 'setofkey:update', blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      new Command('delete', '删除', 'fa fa-trash-o', 'setofkey:delete', blade => this.exeDelete(blade), blade => this.canDelete(blade)),
    ];

    super.ngOnInit();
  }

  initData(): any {
    this.windPowersService.getWindPowersBySchemeId(this.currentEntity.schemeId)
      .then(res => {
        this.bladeData.isLoading = false;
        if (res) {
          this.windPowersList = [...res];
          this.currentWindPower = this.windPowersList[0]
        }
      })
      .catch(err => {
        console.log(err)
      })
  }

  exeAdd(blend: Blade): void {
    let data: BladeData = new BladeData();
    data.id = 'modelGroupingCreate';
    data.title = '创建一个标准曲线';
    data.isClosingDisabled = false;
    let createKeyBlade = this.bladeService.createBlade(StandardCurveEditComponent, data, true, {
      windPowerId: 0,
      schemeId: this.currentEntity.schemeId
    });
    this.bladeService.showBlade(createKeyBlade, this.currentBlade);
  }

  canExeAdd(blend: Blade): boolean {
    return true;
  }

  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = 'standardCurveEdit';
      data.title = '标准曲线';
      let editKeyBlade = this.bladeService.createBlade(StandardCurveEditComponent, data, false, this.currentWindPower);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }

  canExeEdit(blend?: Blade): boolean {
    return this.currentWindPower != null;
  }

  exeDelete(blend: Blade): void {
    let selectedKeys = this.getSelected();
    if (selectedKeys.length > 0) {
      this.dialogService
        .showConfirmationDialog('确认删除', '是否要删除选中项？')
        .subscribe(dialogResult => {
          if (dialogResult == DialogResult.Ok) {
            this.currentBlade.bladeData.isLoading = true;
            this.windPowersService.batchDelete(selectedKeys.map(o => o.windPowerId))
              .then(res => {
                if (res) {
                  super.refresh(this.currentEntity);
                }
              })
              .catch(error => this.bladeData.error = error)
              .then(res => this.bladeData.isLoading = false);
          }
        })
    }
  }

  canDelete(blend?: Blade): boolean {
    return this.getSelected().length > 0;
  }

  /**
   * 获得选中项
   *
   * @returns {Model[]}
   * @memberof StandardCurveListComponent
   */
  getSelected(): WindPowers[] {
    if (this.windPowersList.length > 0) {
      let filter = this.windPowersList.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }

  /**
   * 选中当前风功率
   *
   * @param {WindPowers} selectedKey
   * @memberof StandardCurveListComponent
   */
  onSeletedChange(selectedKey: WindPowers) {
    this.currentWindPower = selectedKey;
  }

  onItemCheack(event: HTMLInputElement, key: WindPowers) {
    key.isChecked = event.checked;
  }

  /**
   * 全选
   *
   * @param {HTMLInputElement} event
   * @memberof StandardCurveListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.windPowersList.length > 0) {
      for (let i = 0; i < this.windPowersList.length; i++) {
        this.windPowersList[i].isChecked = event.checked;
      }
    }
  }
}


// WEBPACK FOOTER //
// ./src/app/module/basic/model/standard-curve/standard-curve-list/standard-curve-list.component.ts
